/**
 * 
 */
package com.ofek.applicationbase.model;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.ofek.applicationbase.model.IModel.ModelListener;

/**
 * @author Ofek
 *
 */
public class AndroidBindUtils {
	public static TextView bind(final TextView view, final StringModel model) {
		model.registerModelChangedListener(new ModelListener<StringModel>() {
			@Override
			public void onModelChanged(final StringModel model) {
				view.setText(model.get());
			}
		});
		return view;
	}
	public static EditText bind(final EditText view, final StringModel model) {
		final String string = model.get();
		if (string!=null) 
			view.setText(string);
		model.registerModelChangedListener(new ModelListener<StringModel>() {
			@Override
			public void onModelChanged(final StringModel model) {
				view.setText(model.get());
			}
		});
		view.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
				model.set(view.getText().toString());
			}

			@Override
			public void beforeTextChanged(final CharSequence s, final int start, final int count,
					final int after) {
			}

			@Override
			public void afterTextChanged(final Editable s) {
			}
		});
		return view;
	}
}
